from flask import jsonify
from app_db import app
from ..model.models import IncomeExpenseClass, AccountType
from ..model.read_model import *

from flask import request


# 收支一级类别
@app.route('/api/income_expense_class', methods=['GET'])
def api_income_expense_class_info():
    info = read_model_income_expense_class()
    return jsonify({'code': 200, 'data': info})


# 收支二级类别
@app.route('/api/income_expense_class/<int:id>', methods=['GET'])
def api_income_expense_class(id):
    # query = IncomeExpenseClass.query.filter_by(parent_id=id).all()
    info = read_model_income_expense_class(parent_id=id)
    return jsonify({'code': 200, 'data': info})


# 账户一级类别
@app.route('/api/common_code', methods=['GET'])
def api_common_code():
    query = read_model_common_code(category_code='account_type')
    return jsonify({'code': 200, 'data': query})


# 账户二级类别
@app.route('/api/account_class/<int:user_id>/<int:id>', methods=['GET'])
def api_account_class(user_id, id):
    user_id = None if id == 3 else user_id	
    query = AccountType.query.filter_by(user_id=user_id).filter_by(type_id=id).filter_by(is_valid=1).all()
    info = [{
            'id': data.id,
            'account_name': data.account_name
        } for data in query] if query else []

    return jsonify({'code': 200, 'data': info})


# 最近12个月的还款金额趋势
@app.route('/api/repayment_recenty/<int:user_id>', methods=['GET'])
def api_repayment_recenty(user_id):
    account_id = int(request.args.get('account_id')) if request.args.get('account_id') else None
    begin_year = int(request.args.get('begin_year')) if request.args.get('begin_year') else None
    begin_month = int(request.args.get('begin_month')) if request.args.get('begin_month') else None
    end_year = int(request.args.get('end_year')) if request.args.get('end_year') else None
    end_month = int(request.args.get('end_month')) if request.args.get('end_month') else None

    account_id = None if account_id == -1 else account_id
    begin_year = None if begin_year == -1 else begin_year
    begin_month = None if begin_month == -1 else begin_month
    end_year = None if end_year == -1 else end_year
    end_month = None if end_month == -1 else end_month


    json_data = repayment_recenty(user_id=user_id, begin_year=begin_year, begin_month=begin_month, end_year=end_year,
                                  end_month=end_month, account_id=account_id)
    months = []
    moneys = []
    for info in json_data:
        months.append(info['month'])
        moneys.append(info['money'])

    return jsonify({'month': months, 'money': moneys})


# 最近12个月的消费金额趋势
@app.route('/api/cost_recenty/<int:user_id>', methods=['GET'])
def api_cost_recenty(user_id):
    income_expense_class_id = int(request.args.get('income_expense_class_id')) if request.args.get(
        'income_expense_class_id') else None
    income_expense_id = int(request.args.get('income_expense_id')) if request.args.get('income_expense_id') else None
    pay_channel_id = int(request.args.get('pay_channel_id')) if request.args.get('pay_channel_id') else None
    account_class_id = int(request.args.get('account_class_id')) if request.args.get('account_class_id') else None
    account_id = int(request.args.get('account_id')) if request.args.get('account_id') else None
    begin_year = int(request.args.get('begin_year')) if request.args.get('begin_year') else None
    begin_month = int(request.args.get('begin_month')) if request.args.get('begin_month') else None
    begin_day = int(request.args.get('begin_day')) if request.args.get('begin_day') else None
    end_year = int(request.args.get('end_year')) if request.args.get('end_year') else None
    end_month = int(request.args.get('end_month')) if request.args.get('end_month') else None
    end_day = int(request.args.get('end_day')) if request.args.get('end_day') else None

    income_expense_class_id = None if income_expense_class_id == -1 else income_expense_class_id
    income_expense_id = None if income_expense_id == -1 else income_expense_id
    pay_channel_id = None if pay_channel_id == -1 else pay_channel_id
    account_class_id = None if account_class_id == -1 else account_class_id
    account_id = None if account_id == -1 else account_id
    begin_year = None if begin_year == -1 else begin_year
    begin_month = None if begin_month == -1 else begin_month
    begin_day = None if begin_day == -1 else begin_day
    end_year = None if end_year == -1 else end_year
    end_month = None if end_month == -1 else end_month
    end_day = None if end_day == -1 else end_day

    category_json_data = cost_recenty(user_id=user_id, pay_channel_id=pay_channel_id,
                                      account_class_id=account_class_id, account_id=account_id,
                                      begin_year=begin_year, income_expense_class_id=income_expense_class_id,
                                      income_expense_id=income_expense_id,
                                      begin_month=begin_month, begin_day=begin_day,
                                      end_year=end_year, end_month=end_month, end_day=end_day)
    dates = []
    moneys = []
    for info in category_json_data:
        dates.append(info['date'])
        moneys.append(info['money'])

    return jsonify({'date': dates, 'money': moneys})


# 剔除还信用卡后的近30日消费趋势
@app.route('/api/cost_recenty_class/<int:user_id>', methods=['GET'])
def api_cost_recenty_class(user_id):
    pay_channel_id = int(request.args.get('pay_channel_id')) if request.args.get('pay_channel_id') else None
    account_class_id = int(request.args.get('account_class_id')) if request.args.get('account_class_id') else None
    account_id = int(request.args.get('account_id')) if request.args.get('account_id') else None
    begin_year = int(request.args.get('begin_year')) if request.args.get('begin_year') else None
    begin_month = int(request.args.get('begin_month')) if request.args.get('begin_month') else None
    begin_day = int(request.args.get('begin_day')) if request.args.get('begin_day') else None
    end_year = int(request.args.get('end_year')) if request.args.get('end_year') else None
    end_month = int(request.args.get('end_month')) if request.args.get('end_month') else None
    end_day = int(request.args.get('end_day')) if request.args.get('end_day') else None

    pay_channel_id = None if pay_channel_id == -1 else pay_channel_id
    account_class_id = None if account_class_id == -1 else account_class_id
    account_id = None if account_id == -1 else account_id
    begin_year = None if begin_year == -1 else begin_year
    begin_month = None if begin_month == -1 else begin_month
    begin_day = None if begin_day == -1 else begin_day
    end_year = None if end_year == -1 else end_year
    end_month = None if end_month == -1 else end_month
    end_day = None if end_day == -1 else end_day

    category_json_data = cost_recenty_struct_class(user_id=user_id, pay_channel_id=pay_channel_id,
                                                   account_class_id=account_class_id, account_id=account_id,
                                                   begin_year=begin_year,
                                                   begin_month=begin_month, begin_day=begin_day,
                                                   end_year=end_year, end_month=end_month, end_day=end_day)

    json_data = {
        'code': 200,
        'data': category_json_data
    }

    return jsonify(json_data)
